//
//  FMDBSingleton.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/26.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "FMDBSingleton.h"

@implementation FMDBSingleton

+(instancetype)FMDBDataBase
{
    static FMDBSingleton *FMDBdB = nil;
    if (FMDBdB == nil)
    {
        FMDBdB = [[FMDBSingleton alloc] init];
    }
    return FMDBdB;
}

-(void)openDB
{
    
    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName=[doc stringByAppendingPathComponent:@"NetEase.sqlite"];
    NSLog(@"fileName = %@",fileName);
    self.db = [FMDatabase databaseWithPath:fileName];
    if ([self.db open])
    {
        NSLog(@"ok");

        //4.创表
        BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS NetEase(number INTEGER PRIMARY KEY AUTOINCREMENT, tname TEXT, title TEXT, digest TEXT, alias TEXT, docid TEXT, ename TEXT);"];
        if (result) {
            NSLog(@"创表成功");
        }else{
            NSLog(@"创表失败");
        }
    }
}

-(void)insertRecommend:(LJYRecEveryDayModel *)Recommend
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO NetEase(tname, title, digest, alias, docid, ename) VALUES('%@', '%@', '%@', '%@', '%@', '%@')", Recommend.tname, Recommend.title, Recommend.digest, Recommend.alias, Recommend.docid, Recommend.ename];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        
        NSLog(@"插入成功");
        
    }else
    {
        NSLog(@"插入失败");
        
    }
}

-(NSMutableArray *)selectMoreRecommend
{
    NSMutableArray *array = [NSMutableArray array];
    
    FMResultSet *resultSet = [self.db executeQuery:@"select * from NetEase"];
    // 循环遍历每一行数据
    while ([resultSet next])
    {
        NSString *tname = [resultSet objectForColumnName:@"tname"];
        NSString *title = [resultSet objectForColumnName:@"title"];
        NSString *ename = [resultSet objectForColumnName:@"ename"];
        LJYRecEveryDayModel *model = [[LJYRecEveryDayModel alloc] init];
        model.tname = tname;
        model.ename = ename;
        model.title = title;
        [array addObject:model];
    }
    return array;
}

-(NSMutableArray *)selectReacommendName
{
    NSMutableArray *array = [NSMutableArray array];
    
    FMResultSet *resultSet = [self.db executeQuery:@"select * from NetEase"];
    // 循环遍历每一行数据
    while ([resultSet next])
    {
        NSString *tname = [resultSet objectForColumnName:@"tname"];
        [array addObject:tname];
    }
    return array;
}

-(void)deleteData:(NSString *)name
{
    
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM NetEase WHERE tname = '%@'", name];
    BOOL res = [self.db executeUpdate:deleteSql];
    
    if (res) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

-(void)dropNetEase
{
    [self.db executeUpdate:@"DROP TABLE IF EXISTS NetEase"];
    
    //4.创表
    BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS NetEase(number INTEGER PRIMARY KEY AUTOINCREMENT, tname TEXT, title TEXT, digest TEXT, alias TEXT, docid TEXT, ename TEXT);"];
    if (result) {
        NSLog(@"创表成功");
    }else{
        NSLog(@"创表失败");
    }
    
}

-(void)closeDB
{
    [self.db close];
}

@end
